import 'package:boss/color_constant.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusPackage/widget/package_panel.dart';
import 'package:boss/ui/page/takeoutManagement/page/deliveryFeeSetting/provider/delivery_provider.dart';
import 'package:boss/ui/page/takeoutManagement/page/widget/plus_row.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'delivery_free_radio.dart';
import 'delivery_rule_row.dart';

class DeliveryRulePanel extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return PackagePanel(
      title: '配送规则',
      titleStyle: TextStyle(
        fontSize: 32.sp,
        color: ColorConstant.ff33,
        fontWeight: FontWeight.w500,
      ),
      headerPadding: EdgeInsets.zero,
      contentPadding: EdgeInsets.zero,
      actions: <Widget>[
        Consumer<DeliveryProvider>(
          builder: (context, provider, _) {
            return DeliveryFreeRadio(
              isSelected: provider.data.isFreeDelivery,
              onChanged: (flag) {
                provider.setFreeDelivery(flag);
              },
            );
          },
        ),
      ],
      children: <Widget>[
        Consumer<DeliveryProvider>(
          builder: (context, provider, _) {
            return Column(
              children: provider.data.takeoutCostSettingFormList
                  .map((item) => DeliveryRuleRow(
                        data: item,
                        key: item.key,
                        onKmChanged: (value) {
                          item.targetValue = int.parse(value);
                        },
                        onPriceChanged: (value) {
                          item.costAmount = AmountUtil.string2Price(value);
                        },
                        onDelete: () {
                          provider.removeDeliveryRule(item);
                        },
                        enabled: !provider.data.isFreeDelivery,
                      ))
                  .toList(),
            );
          },
        ),
        PlusRow(
          onTap: () {
            var provider = Provider.of<DeliveryProvider>(
              context,
              listen: false,
            );
            provider.addDeliveryRule();
          },
        ),
      ],
    );
  }
}
